﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using FlowBasis.Json;

namespace FlowBasis.Web.Mvc
{

    public class PostRpcValueProvider : IValueProvider
    {
        private JObject requestMessage;

        public PostRpcValueProvider(JObject requestMessage)
        {
            this.requestMessage = requestMessage;
        }

        public bool ContainsPrefix(string prefix)
        {
            if (this.requestMessage.ContainsKey(prefix) || prefix == "requestMessage")
            {
                return true;
            }

            return false;
        }

        public ValueProviderResult GetValue(string key)
        {
            object value;
            if (!this.requestMessage.TryGetValue(key, out value))
            {
                if (key == "requestMessage")
                {
                    value = this.requestMessage;
                }
            }

            if (value != null)
            {
                var result = new ValueProviderResult(value, null, System.Globalization.CultureInfo.InvariantCulture);
                return result;
            }
            else
            {
                return null;
            }
        }

    }

}
